{% extends "common/layout_main.html" %}
{% block content %}
<div class="row  border-bottom">
    <div class="col-lg-12">
        <div class="tab_title">
            <ul class="nav nav-pills">
                <li class="current">
                    <a href="{{ url_for('user_page.edit') }}">信息编辑</a>
                </li>
                <li>
                    <a href="{{ url_for('user_page.resetPwd') }}">修改密码</a>
                </li>
            </ul>
        </div>
    </div>
</div>
<div class="row m-t  user_edit_wrap">
    <div class="col-lg-12">
        <h2 class="text-center">账号信息编辑</h2>
        <div class="form-horizontal m-t m-b">
            <div class="form-group">
                <label class="col-lg-2 control-label">账号:</label>
                <div class="col-lg-10">
                    <input type="text" name="login_name" class="form-control" placeholder="请输入账号~~" readonly=""
                           value="{{ current_user.login_name }}">
                </div>
            </div>
            <div class="hr-line-dashed"></div>

            <div class="form-group">
                <label class="col-lg-2 control-label">昵称:</label>
                <div class="col-lg-10">
                    <input type="text" name="nickname" class="form-control" placeholder="请输入昵称" value="{{ current_user.nickname }}">
                </div>
            </div>
            <div class="hr-line-dashed"></div>
			<div class="form-group">
				<label class="col-lg-2 control-label">性别:</label>
				<div class="col-lg-10">
					<input type="text" name="sex" class="form-control" placeholder="请输入性别~~" value="{% if current_user.sex == 1 %}男{% else %}女{% endif %}">
				</div>
			</div>
			<div class="hr-line-dashed"></div>
			<div class="form-group">
				<label class="col-lg-2 control-label">手机:</label>
				<div class="col-lg-10">
					<input type="text" name="mobile" class="form-control" placeholder="请输入手机号~~" value="{{ current_user.mobile }}">
				</div>
			</div>
            <div class="hr-line-dashed"></div>

            <div class="form-group">
                <label class="col-lg-2 control-label">邮箱:</label>
                <div class="col-lg-10">
                    <input type="text" name="email" class="form-control" placeholder="请输入邮箱" value="{{ current_user.email }}">
                </div>
            </div>
            <div class="hr-line-dashed"></div>
            <div class="form-group">
                <div class="col-lg-4 col-lg-offset-2">
                    <button class="btn btn-w-m btn-outline btn-primary save">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block js %}
<script>
	var user_edit = {
		init:function(){
			this.eventBind()
		},
		isPhone:function(phones){
            var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
            return myreg.test(phones)
        },
		eventBind:function(){
            $(".user_edit_wrap .save").click(function(){
                var nickname_target = $(".user_edit_wrap input[name=nickname]");
                var sex_target = $(".user_edit_wrap input[name=sex]");
                var mobile_target = $(".user_edit_wrap input[name=mobile]");
                var email_target = $(".user_edit_wrap input[name=email]");
                var nickname = nickname_target.val()
                var sex = sex_target.val();
                var mobile = mobile_target.val();
                var email = email_target.val()

                if (nickname == "" || nickname.length < 1){
                    common_ops.tip("请输入正确的昵称！",nickname_target)
                    return ;
                }

                if (sex == "" || sex.length != 1){
                    common_ops.tip("请输入正确的性别！",sex_target)
                    return ;
                }
                if (mobile == "" || !user_edit.isPhone(mobile)){
                    common_ops.tip("请输入正确的手机号！",mobile_target)
                    return ;
                }

                if (email == "" || email.length < 6 ){
                    common_ops.tip("请输入正确的邮箱！",email_target)
                    return ;
                }

                $.ajax({
                    url:"/user/edit",
                    type:"POST",
                    data:{"nickname":nickname,"sex":sex,"mobile":mobile,"email":email},
                    dataType:"json",
                    success:function(res){
                        var callback = null
                        if(res.code == 200){
                            callback = function(){
                                window.location.href = window.location.href
                            }
                            common_ops.alert(res.msg,callback)
                        }
                        if(res.code == -1){
                            common_ops.alert(res.msg)
                        }
                    }
                })
            });
		}
	}
    $(document).ready(function(){
        user_edit.init();
    })

</script>
{% endblock %}
